document.addEventListener('DOMContentLoaded', () => {
  const toastContainer = document.querySelector('.toast-container') || createToastContainer();

  document.querySelectorAll('[data-copy]').forEach((btn) => {
    btn.addEventListener('click', () => {
      const target = btn.getAttribute('data-copy');
      const text = document.querySelector(target)?.textContent?.trim();
      if (!text) return;
      navigator.clipboard.writeText(text).then(() => showToast('已复制到剪贴板'));
    });
  });

  document.querySelectorAll('[data-confirm]').forEach((btn) => {
    btn.addEventListener('click', (event) => {
      const message = btn.getAttribute('data-confirm');
      if (!window.confirm(message)) {
        event.preventDefault();
      }
    });
  });

  document.querySelectorAll('[data-toggle]').forEach((btn) => {
    btn.addEventListener('click', () => {
      const target = document.querySelector(btn.getAttribute('data-toggle'));
      if (!target) return;
      target.classList.toggle('active');
    });
  });

  document.querySelectorAll('.lazyload').forEach((section) => {
    setTimeout(() => section.classList.remove('skeleton'), 400);
  });

  function createToastContainer() {
    const container = document.createElement('div');
    container.className = 'toast-container';
    document.body.appendChild(container);
    return container;
  }

  window.showToast = function (message, type = 'default') {
    const toast = document.createElement('div');
    toast.className = `toast toast-${type}`;
    toast.textContent = message;
    toastContainer.appendChild(toast);
    setTimeout(() => {
      toast.style.opacity = '0';
      toast.style.transform = 'translateY(-10px)';
    }, 2600);
    setTimeout(() => toast.remove(), 3200);
  };
});

